package com.yzy.soft.system.freemarker;

import com.yzy.soft.common.persistence.dao.MetadataColumnMapper;
import com.yzy.soft.common.persistence.dao.MetadataDatabaseMapper;
import com.yzy.soft.common.persistence.dao.MetadataTableMapper;
import com.yzy.soft.common.persistence.dao.UrlJsonFieldsStructureMapper;
import com.yzy.soft.common.persistence.model.MetadataColumn;
import com.yzy.soft.common.persistence.model.MetadataDatabase;
import com.yzy.soft.common.persistence.model.MetadataTable;
import com.yzy.soft.common.persistence.model.UrlJsonFieldsStructure;
import com.yzy.soft.system.util.JsonUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class SynchronousJson {
    private DBInfo dbInfo;
    @Autowired
    private MetadataTableMapper metadataTableMapper;
    @Autowired
    private MetadataColumnMapper metadataColumnMapper;

    @Autowired
    private MetadataDatabaseMapper metadataDatabaseMapper;
    @Autowired
    private UrlJsonFieldsStructureMapper urlJsonFieldsStructureMapper;

    public DBInfo getDbInfo() {
        dbInfo = new DBInfo();
        dbInfo.setDriver("com.mysql.jdbc.Driver");
        dbInfo.setUrl("jdbc:mysql://127.0.0.1:3307/codebuilder?characterEncoding=utf8&useSSL=false");
        dbInfo.setUsername("root");
        dbInfo.setPassword("root");
        return dbInfo;
    }

    public DBInfo getDbInfo(MetadataDatabase metadataDatabase) {
        dbInfo = new DBInfo();
        dbInfo.setDriver("com.mysql.jdbc.Driver");
        dbInfo.setUrl(metadataDatabase.getUrl());
        dbInfo.setUsername(metadataDatabase.getUsername());
        dbInfo.setPassword(metadataDatabase.getPassword());
        return dbInfo;
    }

    /**
     * 数据库测试连接
     * @param metadataDatabase
     * @return
     */
    public boolean connectTest(MetadataDatabase metadataDatabase) {
        dbInfo = this.getDbInfo(metadataDatabase);
        JdbcUtil jdbcUtil = new JdbcUtil(dbInfo.getDriver(), dbInfo.getUrl(), dbInfo.getUsername(), dbInfo.getPassword());

        Connection connection = jdbcUtil.getConnection();
        if (null != connection) {
            return true;
        }

        return false;
    }





    public void setFieldsStructuralStorage(List<UrlJsonFieldsStructure> list,int maxLevel) {
        if (null == list || list.size() == 0) {
            return;
        }
        for (int i = 1; i <=maxLevel ;i++) {
            for (int j = 0; j < list.size(); j++) {
                UrlJsonFieldsStructure urlJsonFieldsStructure = list.get(j);
                if(i==urlJsonFieldsStructure.getLevel()){
                    urlJsonFieldsStructureMapper.insert(urlJsonFieldsStructure);
                    if(1!=urlJsonFieldsStructure.getLevel()){
                        UrlJsonFieldsStructure parent=urlJsonFieldsStructureMapper.selectParentId(urlJsonFieldsStructure);
                        urlJsonFieldsStructure.setParentId(parent.getId());
                        urlJsonFieldsStructureMapper.updateParentId(urlJsonFieldsStructure);
                    }

                }
            }
        }
    }

    public void fieldsStructuralStorage(String ss) {
        JsonUtils json = new JsonUtils();
        List<UrlJsonFieldsStructure> domain = new ArrayList<>();
        json.getFieldsStructure(ss, domain);
        setFieldsStructuralStorage(domain,json.getMaxLevel());
    }
}
